using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using Bamboo.Core.Data;
using Bamboo.Core.Common.Collection;
using Bamboo.Core.Common.Logger;
using TinyERP.Core.Business;
using TinyERP.Core.DTO;

namespace TinyERP.Core.View
{
    public class UserGroupListForm : BaseListForm
    {
		private DataGridViewTextBoxColumn nameColumn;
		private DataGridViewTextBoxColumn descriptionColumn;

		public IUserGroupService UserGroupService 
		{
			get
			{
				return (IUserGroupService) ServiceLocator.GetService("UserGroupServiceProxy");
			}
		}
		

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
			this.SuspendLayout();
			this.nameColumn = new DataGridViewTextBoxColumn() ;
			this.descriptionColumn = new DataGridViewTextBoxColumn() ;
			this.ResumeLayout(false);

        }

        #endregion

        
        
        private void SetupDataGridView()
        {
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
			this.nameColumn
			,
			this.descriptionColumn
            });
            
			this.nameColumn.DataPropertyName = "Name";
			this.nameColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("Name", "TinyERP.Core");
			this.nameColumn.Name = "nameColumn";
			this.nameColumn.Tag = "name";
			this.nameColumn.ReadOnly = true;
			this.nameColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.descriptionColumn.DataPropertyName = "Description";
			this.descriptionColumn.HeaderText = ModuleResourceManager.GetInstance().GetString("Description", "TinyERP.Core");
			this.descriptionColumn.Name = "descriptionColumn";
			this.descriptionColumn.Tag = "description";
			this.descriptionColumn.ReadOnly = true;
			this.descriptionColumn.SortMode = DataGridViewColumnSortMode.Programmatic;

            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.MultiSelect = false;
            this.dataGridView.ResourceModuleKey = "TinyERP.Core";
            this.dataGridView.AllowUserToOrderColumns = true;
            this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            InitializeSortKeyMap();
        }
        
        private void InitializeSortKeyMap() 
        {
        }

		public UserGroupListForm() : base()
        {
            InitializeComponent();
			SetupDataGridView();
        }
        

		public override void OnNew()
        {
            UserGroupEditInfoForm editForm = new UserGroupEditInfoForm();
            editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            }; 
            DialogDecorator dialog = new DialogDecorator(editForm);
            dialog.ShowDialog();
        }
        

		public override void OnDelete()
        {
			if (this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            UserGroupDTO userGroup = (UserGroupDTO)this.dataGridView.SelectedRows[0].DataBoundItem;
            try 
			{
                if (MessageBox.Show(CommonMessage.GetDeleteConfirmMessage("UserGroup"),
                CommonMessage.GetConfirmationText(), MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
					UserGroupService.DeleteUserGroupByID(userGroup.ID);
					MessageBox.Show(CommonMessage.GetDeleteSuccessMessage("UserGroup"));
					LoadGridData();
				}
			}
			catch(Exception ex) 
			{
				LogManager.BusinessLog.Debug(ex);
				MessageBox.Show(CommonMessage.GetCanNotDeleteError("UserGroup"));
			}
        }
        
        
        public override void OnEdit()
        {
            if (this.dataGridView.SelectedRows == null || this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            UserGroupDTO userGroupDTO = (UserGroupDTO) this.dataGridView.SelectedRows[0].DataBoundItem;
            UserGroupEditInfoForm editForm = new UserGroupEditInfoForm();
            editForm.UserGroup = userGroupDTO;
			editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            };            
            DialogDecorator dialog = new DialogDecorator(editForm);
            dialog.StartPosition = FormStartPosition.CenterParent;
            dialog.ShowDialog(this);
        }
        

        

        
		protected override void LoadGridViewSearchData(IList<SearchCondition> searchConditions, Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
			long totalRecord = 0;
            IList userGroupList = UserGroupService.SearchUserGroup(paging.CurrentPage, paging.PageSize, searchConditions, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            SortableBindingList<UserGroupDTO> sortableUserGroupList = SortableBindingList<UserGroupDTO>.CreateSortableBindingList(userGroupList);
            this.dataGridView.DataSource = sortableUserGroupList;
        }

        protected override void LoadGridViewDefaultData(Bamboo.Core.Data.SortDirection sortDirection, string columnName) 
        {
        	long totalRecord = 0;
            IList userGroupList = UserGroupService.GetAllUserGroup(paging.CurrentPage, paging.PageSize, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            SortableBindingList<UserGroupDTO> sortableUserGroupList = SortableBindingList<UserGroupDTO>.CreateSortableBindingList(userGroupList);
            this.dataGridView.DataSource = sortableUserGroupList;
        }        

    }
}
